package payment.mobilepos.unionpay.com.demo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import payment.mobilepos.unionpay.com.R;


public class CashierKeyboard extends FrameLayout implements View.OnClickListener {

    /**
     * 最大输入长度, (金额最大长度是7,不包含2位小数点)
     */
    private static final int MAX_CHARS = 7;

    /**
     * 按清空键标识符
     */
    private static final String CLEAN = "CLEAN";

    /**
     * 按退格键标识符
     */
    private static final String DELETE = "DELETE";

    /**
     * 按支付键标识符
     */
    private static final String PAYEE = "PAYEE";

    private String mInputText;

    public interface OnInputTextChangedListener {
        void onInputTextChanged(String text);
    }

    private OnInputTextChangedListener mOnInputTextChangedListener;

    public interface OnPayeeViewClickedListener {
        void onPayeeViewClicked();
    }

    private OnPayeeViewClickedListener mOnPayeeViewClickedListener;

    public CashierKeyboard(Context context) {
        super(context);
        init(context);
    }

    public CashierKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.layout_cashier_keyboard, null);

        Button btnKey0 = (Button) v.findViewById(R.id.btnKey_0);
        btnKey0.setOnClickListener(CashierKeyboard.this);

        Button btnKey1 = (Button) v.findViewById(R.id.btnKey_1);
        btnKey1.setOnClickListener(CashierKeyboard.this);

        Button btnKey2 = (Button) v.findViewById(R.id.btnKey_2);
        btnKey2.setOnClickListener(CashierKeyboard.this);

        Button btnKey3 = (Button) v.findViewById(R.id.btnKey_3);
        btnKey3.setOnClickListener(CashierKeyboard.this);

        Button btnKey4 = (Button) v.findViewById(R.id.btnKey_4);
        btnKey4.setOnClickListener(CashierKeyboard.this);

        Button btnKey5 = (Button) v.findViewById(R.id.btnKey_5);
        btnKey5.setOnClickListener(CashierKeyboard.this);

        Button btnKey6 = (Button) v.findViewById(R.id.btnKey_6);
        btnKey6.setOnClickListener(CashierKeyboard.this);

        Button btnKey7 = (Button) v.findViewById(R.id.btnKey_7);
        btnKey7.setOnClickListener(CashierKeyboard.this);

        Button btnKey8 = (Button) v.findViewById(R.id.btnKey_8);
        btnKey8.setOnClickListener(CashierKeyboard.this);

        Button btnKey9 = (Button) v.findViewById(R.id.btnKey_9);
        btnKey9.setOnClickListener(CashierKeyboard.this);

        Button btnKeyDot = (Button) v.findViewById(R.id.btnKey_dot);
        btnKeyDot.setOnClickListener(CashierKeyboard.this);

        Button btnKeyClean = (Button) v.findViewById(R.id.btnKey_clean);
        btnKeyClean.setOnClickListener(CashierKeyboard.this);

        Button btnKeyDelete = (Button) v.findViewById(R.id.btnKey_del);
        btnKeyDelete.setOnClickListener(CashierKeyboard.this);

        Button btnKeyPayee = (Button) v.findViewById(R.id.btnKey_payee);
        btnKeyPayee.setOnClickListener(CashierKeyboard.this);

        addView(v, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        mInputText = "0";
    }

    public void setOnInputTextChangedListener(OnInputTextChangedListener onInputTextChangedListener) {
        this.mOnInputTextChangedListener = onInputTextChangedListener;
    }

    public void setOnPayeeViewClickedListener(OnPayeeViewClickedListener onPayeeViewClickedListener) {
        this.mOnPayeeViewClickedListener = onPayeeViewClickedListener;
    }

    @Override
    public void onClick(View v) {
        String inputContent = "";
        switch (v.getId()) {
            case R.id.btnKey_0:
                inputContent = "0";
                break;
            case R.id.btnKey_1:
                inputContent = "1";
                break;
            case R.id.btnKey_2:
                inputContent = "2";
                break;
            case R.id.btnKey_3:
                inputContent = "3";
                break;
            case R.id.btnKey_4:
                inputContent = "4";
                break;
            case R.id.btnKey_5:
                inputContent = "5";
                break;
            case R.id.btnKey_6:
                inputContent = "6";
                break;
            case R.id.btnKey_7:
                inputContent = "7";
                break;
            case R.id.btnKey_8:
                inputContent = "8";
                break;
            case R.id.btnKey_9:
                inputContent = "9";
                break;
            case R.id.btnKey_dot:
                inputContent = ".";
                break;
            case R.id.btnKey_clean:
                inputContent = CLEAN;
                break;
            case R.id.btnKey_del:
                inputContent = DELETE;
                break;
            case R.id.btnKey_payee:
                inputContent = PAYEE;
                break;
        }

        int length = mInputText.length();

        if (CLEAN.equals(inputContent)) {
            mInputText = "0";
        } else if (DELETE.equals(inputContent)) {
            int indexBounds = mInputText.length() - 1;
            if (indexBounds > 0) {
                mInputText = mInputText.substring(0, indexBounds);
            } else {
                mInputText = "0";
            }
        } else if (PAYEE.equals(inputContent)) {
            if (mOnPayeeViewClickedListener != null) {
                mOnPayeeViewClickedListener.onPayeeViewClicked();
            }
            return;
        } else {
            if (".".equals(inputContent)) {
                // 输入的数字1~9
                if (length != 0 && length <= MAX_CHARS) {
                    if (!mInputText.contains(".")) {
                        mInputText += inputContent;
                    }
                }
            } else {
                if (mInputText.contains(".")) {
                    int pointIndex = mInputText.indexOf(".");
                    if ((length - pointIndex) < 3) {
                        mInputText += inputContent;
                    }
                } else {
                    if (length < MAX_CHARS) {
                        if (mInputText.equals("0")) {
                            if (inputContent.equals("0")) {
                                mInputText = "0";
                            } else {
                                mInputText = inputContent;
                            }
                        } else {
                            mInputText += inputContent;
                        }
                    }
                }
            }
        }

        if (mOnInputTextChangedListener != null) {
            mOnInputTextChangedListener.onInputTextChanged(mInputText);
        }
    }

}
